﻿using UnityEngine;

namespace Pisces
{
	/**
	 * 特效 
	 * 因为特效可以有多种实现方式，例如：粒子、Spine、序列帧
     * created by wangqiang
    **/
	public abstract class AbstractEffect : MonoBehaviour, InterfaceEntityLiftcycle
	{
		/// <summary>
		/// 粒子的唯一id
		/// </summary>
		private int id;
		
		/// <summary>
		/// 资源的prefab名称
		/// </summary>
		protected string prefabName;

		/// <summary>
		/// 特效的prefab
		/// </summary>
		private GameObject prefab;

		/// <summary>
		/// 特效状态
		/// </summary>
		private EffectStatus effectStatus;

		/// <summary>
		/// 父节点
		/// </summary>
		private GameObject parent;

		public void onInit()
		{
			if (prefabName == null)
			{
				PiscesLog.error("粒子的prefabName为空，无法加载粒子");
				return;
			}

			effectStatus = EffectStatus.INIT;
			
			AssetModule.getSingleton().loadAssetByAsyncAssetName<GameObject>(prefabName, onPrefabLoaded);

			effectStatus = EffectStatus.LOADING;
		}

		private void onPrefabLoaded(GameObject gameObject)
		{
			if (prefab != null)
			{
				AssetModule.getSingleton().releaseAsset(gameObject);
				effectStatus = EffectStatus.DESTROY;
				return;
			}

			effectStatus = EffectStatus.LOADED;

			this.prefab = gameObject;
			
			onLoaded();
			
			onAwake();

			effectStatus = EffectStatus.PLAYING;
		}

		public void onLoaded()
		{
		}

		public void onAwake()
		{
		}

		public void onUpdate(float deltaTime)
		{
		}

		public void onDestroy()
		{
			if (this.prefab != null)
			{
				AssetModule.getSingleton().releaseAsset(this.prefab);
				this.prefab = null;
			}
		}

		public EffectStatus EffectStatus
		{
			get => effectStatus;
			set => effectStatus = value;
		}
		
		public int ID
		{
			get => id;
			set => id = value;
		}

		public string PrefabName
		{
			get => prefabName;
			set => prefabName = value;
		}

		public GameObject Parent
		{
			get => parent;
			set => parent = value;
		}
	}
}